package com.stylefeng.guns.modular.channel_promote_info.service.impl;

import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.apple.vo.Paging;
import com.stylefeng.guns.modular.channel_manage.dao.ChannelInfoMapper;
import com.stylefeng.guns.modular.channel_manage.model.ChannelPromoteDO;
import com.stylefeng.guns.modular.channel_promote_info.bo.ChannelDeviceCountBO;
import com.stylefeng.guns.modular.channel_promote_info.dao.ChannelPromoteInfoMapper;
import com.stylefeng.guns.modular.channel_promote_info.model.ChannelSignCountDO;
import com.stylefeng.guns.modular.channel_promote_info.model.ChannelSignDO;
import com.stylefeng.guns.modular.channel_promote_info.service.IChannelPromoteInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-06-29
 */
@Service
public class ChannelPromoteInfoServiceImpl implements IChannelPromoteInfoService {

    @Resource
    private ChannelPromoteInfoMapper promoteInfoMapper;

    /**
     * 渠道签包数据展示
     * @return
     * @param page
     * @param limit
     */
    @Override
    public Object list(Integer page, Integer limit) {
        int start = (page > 0?page -1:1) * limit;
        Paging<ChannelSignDO> paging = new Paging<>();
        List<ChannelSignDO> signDOList = promoteInfoMapper.findChannelSignInfo(ShiroKit.isAdmin()?null:ShiroKit.getUser().getId(),start,limit);

        Integer totalCount = promoteInfoMapper.findChannelSignTotalCount(ShiroKit.isAdmin()?null:ShiroKit.getUser().getId());

        paging.setTotal(totalCount.longValue());
        paging.setItems(signDOList);

        return paging;
    }

    /**
     * 获取渠道签包数量信息
     * @return
     */
    @Override
    public Object getChannelSignCount(String beginTime,String endTime) {
        List<ChannelSignCountDO> doList =
                promoteInfoMapper.findChannelSignCount(ShiroKit.isAdmin()?null:ShiroKit.getUser().getId(),beginTime,endTime);
        String[] channelNames = new String[doList.size()];
        Integer[] deviceCounts = new Integer[doList.size()];
        List<String> nameArr = new ArrayList<>();
        List<Integer> countArr = new ArrayList<>();

        doList.forEach(c->{
            nameArr.add(c.getChannelName());
            countArr.add(c.getDeviceCount());
        });
        nameArr.toArray(channelNames);
        countArr.toArray(deviceCounts);

        return ChannelDeviceCountBO.builder().namesArr(channelNames).countArr(deviceCounts).build();
    }
}
